package com.funo.business.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.funo.business.dao.CompanyDao;
import com.funo.business.entities.Company;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.tools.StringTool;
import com.funo.core.base.BaseDao;
import com.funo.main.entities.User;

@Repository("companyDao")
public class CompanyDaoImpl extends BaseDao<Company, String> implements CompanyDao{
	
	private Criteria getCriteria(Company company){
		Criteria criteria = super.getCriteria();
		if (company!=null) {
			User user = company.getUser();
			if (user!=null) {
				criteria.add(Restrictions.eq("user", user));
			}
			criteria.add(Restrictions.eq("isValid", new Integer(1)));
			criteria.addOrder(Order.desc("insertDt"));
		}
		return criteria;
	}

	@Override
	public Company getCompany(String userId) {
		// TODO Auto-generated method stub
		List<Company> list = super.findByHql("from Company where user.id=?", userId);
		return list.size()==1?list.get(0):null;
	}

	@Override
	public List<Company> pageList(QueryCondition condition, Company company) {
		// TODO Auto-generated method stub
		Criteria criteria = getCriteria(company);
		criteria.setFirstResult(condition.getStart());
		criteria.setMaxResults(condition.getLimit());
		return super.findByCriteria(criteria);
	}

	@Override
	public Integer count(QueryCondition condition, Company company) {
		// TODO Auto-generated method stub
		Criteria criteria = getCriteria(company);
		return super.countByCriteria(criteria);
	}


}
